var safeBottom = uni.getSystemInfoSync().safeAreaInsets.bottom
// #ifdef APP-NVUE
import {
	mapState,
	mapGetters
} from 'vuex'
import mixin from '@/common/mixins/mixin.js'
// #endif
import {
	Calc
} from "@/common/utils/utils.js";
import {
	GoodsCenterApi
} from '@/api/serves.js'
export default {
	// #ifdef APP-NVUE
	mixins: [mixin], // 使用mixin
	// #endif
	data() {
		return {
			calc: Calc,
			safeBottom: safeBottom,
			goodsType:'', //商品类型   5 为商圈商品
			itemId: '', // 商品id
			details: {}, // 商品详情数据
			attrList: [], // 商品规格分类展示
			skuList: [], // 商品规格组合
			checkedSkuId: '', // 已选的规格skuid
			specInfo: {}, // 选中规格信息
			buyNum: 1, // 购买数量
			totalNum : '', // 商圈商户购物车数量
			evaluation: { // 评论 只显示一条
				total: '',
				info: null,
			},
		}
	},
	onLoad(options) {
		console.log('options',options);
		this.itemId = options.goods_id
		this.goodsType = options.goodsType
		console.log('this.goodsType',this.goodsType);
		if (options && options.username && !uni.getStorageSync("token")) {
			uni.setStorageSync("tjid", options.username)
		}
	},
	watch: {
		attrList: {
			handler(newVal, oldVal) {
				// console.log('attrList', newVal);
				if (!this.skuList.length) return
				let info = []
				for (let item of this.attrList) {
					if (!item.checkedId) break
					info.push(item.checkedId)
				}
				if (info.length == this.attrList.length) {
					this.checkedSkuId = info.join('_')
					for (let v of this.skuList) {
						if (v.spec_sku_id == this.checkedSkuId) {
							this.specInfo = v.form
						}
					}
				}
				if(this.checkedSkuId){
					this.specInfo = this.details.spec.filter(item=>item.spec_sku_id == this.checkedSkuId)[0]
					// 防止多规格商品没有上传规格图片
					if(!!this.specInfo.spec_image){ 
						this.specInfo.imgshow = this.$_BASE_IMG_API + this.specInfo.spec_image
					}else{
						this.specInfo.imgshow = this.details.image
					}
				}
				
				// console.log("this.specInfo",this.specInfo);
				// console.log('this.checkedSkuId', this.checkedSkuId);
			},
			deep: true,
			immediate: true
		},
		details:{
			handler(){
				console.log('this.goodsType',this.goodsType);
				console.log('this.details.merch_id',this.details);
				console.log('商户id',222);
				if(this.goodsType == 5 && this.details.merch_id) {
					this.getCartNum()
				} else{
					this.$store.dispatch('user/getUserCartsInfo', {
						isGetAll: true,
						goods_type:1
					})
				}
			},deep:true
		}
	},
	computed: {
		titleInfo() {
			return {
				goods_name: this.details.goods_name,
				goods_sales: this.details.goods_sales,
				stock_num: this.details.spec && this.details.spec[0] && this.details.spec[0].stock_num || 0,
				address: this.details.merch_info && this.details.merch_info.address_city,
				merch_id: this.details.merch_id
			}
		},
	},
	onShareAppMessage(res) {
		// console.log("查看数据", this.userInfo.username);
		return {
			title: '共产星球分享',
			path: '/pages/goodsDetail/commonGoodsDetail/commonGoodsDetail?goods_id=' + this.itemId + '&username=' + this
				.userInfo.username,
			imageUrl: "https://ssjh.jifenma.com/assets/img/logo_gxxq.png",
			desc: "我正在使用共产星球购物，赶紧跟我一起来体验！"
		}
	},
	onShow() {
		if (this.itemId) this._initData()
	},
	methods: {
		_initData() {
			this._goodsdetail() // 获取商品详情
			this._goodsEvaluate() // 获取商品评价
			if (this.isLogin) this.$store.dispatch('user/getUserCartsInfo', {
				isGetAll: true
			})
		},
		// 获取商品详情
		_goodsdetail() {
			this.checkedSkuId = ''
			uni.showLoading()
			GoodsCenterApi.goodsdetail({
				goods_id: this.itemId
			}).then(res => {
				console.log('获取商品详情', res);
				uni.hideLoading()
				if (res.code == 1) {
					let result = res.data
					this.details = result.detail
					if (result.specData) { 
						this.specInfo = result.specData.spec_list[0].form
						// 防止多规格商品没有上传规格图片
						if(!result.specData.spec_list[0].form.imgshow){
							this.specInfo.imgshow = result.detail.image
						}
						let list = result.specData.spec_attr
						list.map(item => {
							item.checkedId = ''
						})
						this.attrList = list
						this.skuList = result.specData.spec_list
					} else {
						this.checkedSkuId = this.details.spec[0].spec_sku_id
						this.specInfo = this.details.spec[0]
						this.specInfo.imgshow = this.details.image
					}
					console.log(5858, this.specInfo);
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			})
		},
		// 获取商品评价
		_goodsEvaluate() {
			GoodsCenterApi.goods_comments({
				goods_id: this.itemId
			}).then(res => {
				// console.log('获取商品评价', res);
				if (res.code == 1) {
					this.evaluation.total = res.totalpage
					this.evaluation.info = res.data[0]
				}
			})
		},
		// 加入购物车
		addToCart(buyNum) {
			if (!this.isLogin) return this._login()
			for (let item of this.attrList) {
				if (!item.checkedId) return uni.showToast({
					title: `请选择${item.group_name}`,
					icon: 'none'
				})
			}
			let params = {
				goods_num: buyNum,
				goods_id: this.itemId,
				goods_sku_id: this.checkedSkuId
			}
			// console.log(params);
			uni.showLoading()
			GoodsCenterApi.addCart(params).then(res => {
				// console.log('商品数量加', res);
				uni.hideLoading()
				this.$refs.popup.close()
				if (res.code == 1) {
					uni.showToast({
						title: '加入成功'
					})
					if(this.goodsType == 5 ){
						this.getCartNum() // 刷新购物车数量
					}else{
						this.$store.dispatch('user/getUserCartsInfo', {
							isGetAll: true,
							goods_type:1
						})
					}
				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			})
		},
		// 购买 跳转下单页面
		buy(buyNum) {
			if (!this.isLogin) return this._login()
			for (let item of this.attrList) {
				if (!item.checkedId) return uni.showToast({
					title: `请选择${item.group_name}`,
					icon: 'none'
				})
			}
			let params = [{
				goods_id: this.itemId,
				goods_num: buyNum,
				goods_sku_id: this.checkedSkuId,
				merch_id: this.details.merch_id,
				is_special: this.details.is_special
			}]
			// console.log('params', params);
			
			
			this.$refs.popup.close()
			uni.$emit('clearCheckedId')
			if(this.goodsType == 5){
				// console.log("去商圈下单页");
				// let params = [{
				// 	goods_id: this.itemId,
				// 	goods_sku_id: this.checkedSkuId,
				// 	merch_id: this.details.merch_id,
				// }]
				uni.navigateTo({
					url: '/pages/supplierPage/supplierGoodsOrderAdd/supplierGoodsOrderAdd?type=common&orderInfo=' +
						JSON.stringify(params) + '&goodsType=5',
					fail: (err) => {
						console.log(err, "跳转登录回调");
					}
				})
			}else{
				// console.log("去线上下单页");
				uni.navigateTo({
					url: '/pages/goodsDetail/goodsOrderAdd/goodsOrderAdd?type=common&orderInfo=' + JSON.stringify(
						params)
				})
			}
		},
		popupChange(e) {
			if (!e.show) {
				// this.checkedSkuId = ''
				this.attrList.map(v => {
					v.checkedId = ''
				})
			}
		},
		// 跳转评论列表
		toDiscuss() {
			uni.navigateTo({
				url: '/pages/goodsDetail/goodsDiscuss/goodsDiscuss?itemId=' + this.itemId
			})
		},
		couponClick() {
			uni.navigateTo({
				url: '/pages/mine/mineTools/couponCenter/couponCenter?id=' + this.details.merch_id
			})
		},
		// 收藏 或取消收藏 商品
		collect() {
			uni.showLoading()
			let flg = this.details.is_collect
			let fun = flg ? 'notcollectGoods' : 'collectGoods'
			GoodsCenterApi[fun]({
				id: this.details.goods_id,
				type: 1
			}).then(res => {
				// console.log('收藏商品', res);
				if (res.code == 1) {
					this._goodsdetail()
					uni.hideLoading()
					flg ? uni.showToast({
						title: '取消收藏成功'
					}) : uni.showToast({
						title: '收藏成功'
					})

				} else {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			})
		},
		// 规格选择
		checkedIds(obj) {
			this.attrList[obj.index].checkedId = obj.item_id
		},
		getCartNum(){
			console.log('商户id',111);
			GoodsCenterApi.getlists({
				goods_type: 5, 
				merch_id: this.details.merch_id
			}).then(res=>{
				this.totalNum = res.data?.order_total_num || 0
				console.log('商圈商户购物车总数',this.totalNum);
			})
		}
	}
}